package com.nondo.dean.product.service;

import com.nondo.dean.common.domain.BaseEntity;
import com.nondo.dean.common.tool.StringUtils;
import com.nondo.dean.product.domain.*;
import com.nondo.dean.product.mapper.BrandMapper;
import com.nondo.dean.product.mapper.ProductMapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/3/28
 */
@Component
public class BrandService {
    @Autowired
    BrandMapper brandMapper;

    @Autowired
    ProductMapper productMapper;

    /**
     * 添加
     *
     * @param brand
     * @return
     */
    public int add(Brand brand) {
        return this.brandMapper.insertSelective(brand);
    }

    /**
     * 更新
     *
     * @param brand
     * @return
     */
    @Transactional
    public int update(Brand brand) {
        return this.brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * 删除
     *
     * @param brand
     * @return
     */
    public int delete(Brand brand) {
        brand.setDelFlag(Brand.DEL_FLAG_DELETE);
        return this.brandMapper.updateByPrimaryKeySelective(brand);
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    public Brand select(Integer id) {
        return this.brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据名字查询
     *
     * @param name
     * @return
     */
    public Brand selectByName(String name) {
        BrandExample brandExample = new BrandExample();
        brandExample.createCriteria().andNameEqualTo(name).andDelFlagEqualTo(BaseEntity.DEL_FLAG_NORMAL);

        List<Brand> brandList = this.brandMapper.selectByExample(brandExample);

        return CollectionUtils.isNotEmpty(brandList) ? brandList.get(0) : null;
    }

    /**
     * 获取列表
     *
     * @param offset
     * @param limit
     * @param brand
     * @return
     */
    public List<Brand> getList(int offset, int limit, Brand brand) {
        BrandExample example = new BrandExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        BrandExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, brand);
        return brandMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 数据统计
     *
     * @param brand
     * @return
     */
    public Long count(Brand brand) {
        BrandExample example = new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, brand);
        return brandMapper.countByExample(example);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param brand
     */
    void setCriteria(BrandExample.Criteria criteria, Brand brand) {
        if (brand != null) {
            if (brand.getId() != null) {
                criteria.andIdEqualTo(brand.getId());
            }
            if (StringUtils.isNoneBlank(brand.getName())) {
                criteria.andNameEqualTo(brand.getName());
            }
        }
        criteria.andDelFlagEqualTo(Brand.DEL_FLAG_NORMAL);
    }

    /**
     * 检查产品下面是否还有品牌关联
     *
     * @param brand
     * @return
     */
    public int checkProduct(Brand brand) {
        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andBrandIdEqualTo(brand.getId());
        List list = this.productMapper.selectByExample(productExample);
        return list.size();
    }
}
